package lecode;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

public class 寻找最大葫芦 {

    public static void main(String[] args) {
        System.out.println(Arrays.equals(solution(9, 34, new int[]{6, 6, 6, 8, 8, 8, 5, 5, 1}), new int[]{8, 5}));
        System.out.println(Arrays.equals(solution(9, 37, new int[]{9, 9, 9, 9, 6, 6, 6, 6, 13}), new int[]{6, 9}));
        System.out.println(Arrays.equals(solution(9, 40, new int[]{1, 11, 13, 12, 7, 8, 11, 5, 6}), new int[]{0, 0}));
        System.out.println(Arrays.equals(solution(6, 50, new int[]{13, 13, 13, 1, 1, 1}), new int[]{1, 13}));
    }

    public static int[] solution(int n, int max, int[] array) {
        // 牌最多只有14张，利用数组下标来表示牌值，val表示出现的次数 1放到14的位置，便于找最大值
        int[] temp = new int[15];
        for (int val : array) {
            if (val == 1) {
                temp[14]++;
            } else {
                temp[val]++;
            }
        }

        System.out.println(Arrays.toString(temp));

        // 从后边开始找出现次数大于等于3的排
        for (int i = 14; i >= 2; i--) {
            if (temp[i] >= 3) {
                int n3val = (i == 14) ? 1 : i;
                // 从后边开始找出现次数大于等于2的排
                for (int j = 14; j >= 2; j--) {
                    int n2val = (j == 14) ? 1 : j;
                    if (temp[j] >= 2 && n3val != n2val) {
                        // 找到一个组合，判断 是否满足最大值限制，满足则返回
                        if ((n3val * 3 + n2val * 2) <= max) {
                            return new int[]{n3val, n2val};
                        }
                    }
                }
            }
        }
        return new int[]{0, 0};
    }




}
